import {setCookie} from "as-common";
import {LoginType} from "../repo/env/Constants";

export const _isMobile = () => navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);

export const getClient = () => 'm';// _isMobile() ? 'm' : 'pc'

export const isMobile = () => !!_isMobile();

export const u = navigator.userAgent;
export const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;//android终端
export const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

/**
 *  @param num: 需要显示的数字
 *  @param length: 需要不全的长度
 *  数字补全不足补零
 */
export const preFixInteger = (num, length) => (Array(length).join('0') + num).slice(-length);

export const isChinese = (str) => {
  let pattern = new RegExp("[\u4E00-\u9FA5]+")
  return pattern.test(str)
}

export const isEnglish = (str) => {
  let pattern = new RegExp("[A-Za-z]+")
  return pattern.test(str)
}

export const isDigital = (str) => {
  let pattern = new RegExp("[0-9]+")
  return pattern.test(str)
}

export const secondToDate = (result) => {
  let m = Math.floor((result / 60 % 60));
  let s = Math.floor((result % 60));
  return result = m + ':' + (s < 10 ? '0' + s : s)
}


/**
 * @description 这是一个保存视频播放的秒数.
 * key     userId_courseId
 * value   seconds
 * @param {key} key
 * @param {seconds}  seconds
 *
 * */
export const recordPlaySeconds = function (key, seconds) {
  console.log(key, seconds);
  setCookie(key, seconds, 'd30');
};

/**
 * @description 函数节流
 * @param {Function} fn 函数
 * @param {Number} wait 延迟执行毫秒数,默认200
 */
export const throttle = function (fn, wait) {
  let pre = Date.now();
  return function () {
    let context = this;
    let args = arguments;
    let now = Date.now();
    if (now - pre >= wait) {
      fn.apply(context, args);
      pre = Date.now();
    }
  };
};



let agent = navigator.userAgent;
export let isIOS = !!agent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);     //判断是否是 iOS终端




